package com.sinven.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sinven.pojo.User;
import com.sinven.pojo.UserType;
import com.sinven.service.UserService;

@Controller
public class CeShiController {

	@Autowired
	private UserService userService;

	
	
	@GetMapping("login")
	public String login() {
		return "pages/login";
	}
	
	
	@PostMapping("login")
	@ResponseBody
	public User dologin(User user, HttpSession session,Model model) {
		System.out.println(user.getU_name());
		System.out.println(user.getU_password());
		try {
			
			User user1 = userService.login(user);
			System.out.println(user1.getU_name());
			if (user1 != null) {
				System.out.println(user1.getU_type().getU_tid());
				session.setAttribute("user", user1);
				return user1;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("失败");
		return null;
	}
	

	
	@PostMapping("panduan")
	@ResponseBody
	public boolean dopanduan(String u_name) {
		try {
			boolean res=userService.SelectByUname(u_name);
			if(res==false) {
				System.out.println("该用户不存在");
				return true;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return false;
	}
	
	@RequestMapping({"indexs"})
	public String htlogin(String u_name) {
		
		return "pages/indexs";
	}
}
